/*
 * 此页面主要是注意form 这个变量是根据businessOpportunitiesView.jsp页面来的
 * */
Ext.define('App.crm.contractsList', {
    extend      : 'Ext.panel.Panel',
    alias       : 'widget.contractsListPanel',
    layout:'fit',
    autoScroll:true,
    autoHeight:true,
    initComponent : function() {
    	var grid,  selModel,  searchFlds;
    		searchFlds = new Ext.util.MixedCollection();
        var	toolbar = new Ext.Toolbar({
            enableOverflow : true,
            items : [ {
                text : "新增",
                iconCls : 'addIcon',
                handler : function() {
                    var  contractsForm = Ext.create('App.crm.contractsForm');
                    contractsForm.show();
                    contractsForm.setTitle('新增');
                    if(form){
                    contractsForm.down('form').getForm().findField('customerName').setValue(form.getForm().findField("company").getValue());
                    contractsForm.down('form').getForm().findField('customerId').setValue( form.getForm().findField("id").getValue());
                    }
                }
            }, '-', {
                text : "编辑",
                iconCls : 'editIcon',
                handler : function() {
                    var sm = Ext.getCmp('contractsGrid').getSelectionModel();
                    var records = sm.getSelection();
                    if (records.length <= 0) {
                        Ext.Msg.alert('错误', "未选中记录，不能修改！");
                        return;
                    }
                    var  contractsForm = Ext.create('App.crm.contractsForm');
                    contractsForm.show();
                    contractsForm.setTitle('编辑');
                    var data = records[0].raw;
                    contractsForm.down('form').getForm().loadRecord(records[0]);
                    contractsForm.down('form').getForm().findField('customerName').setValue(data.customers.company);
                    contractsForm.down('form').getForm().findField('customerId').setValue(data.customers.id);
                    contractsForm.down('form').getForm().findField('businessOpportunitiesName').setValue(data.businessOpportunities.bussName);
                    contractsForm.down('form').getForm().findField('businessOpportunitiesId').setValue(data.businessOpportunities.id);
                    if(data.startDate!=null){
                        contractsForm.down('form').getForm().findField('startDate').setValue( new Date(data.startDate).format('yyyy-MM-dd'));
                        contractsForm.down('form').getForm().findField('endDate').setValue( new Date(data.endDate).format('yyyy-MM-dd'));
                }
                    if(data.customersSigningDate!=null){
                        contractsForm.down('form').getForm().findField('customersSigningDate').setValue( new Date(data.customersSigningDate).format('yyyy-MM-dd'));
                    }
                    if(data.companySignatureDate!=null){
                        contractsForm.down('form').getForm().findField('companySignatureDate').setValue( new Date(data.companySignatureDate).format('yyyy-MM-dd'));
                    }
                }
            }, '-', {
                text : "删除",
                iconCls : 'deleteIcon',
                handler : function() {
                    Ext.getCmp('contractsGrid').deleteRecord();
                }
            }, "->",  , {
                text: "搜索",
                iconCls : 'searchIcon',
                handler: function () {
                    grid.load();
                }
            } ]
        });
        grid =	Ext.create('gridView',{
            remoteSort : true,
            forceFit: true,
            gridId :  'contractsGrid',
            checkboxSelectionModel:true,
            searchFlds : searchFlds,
            dataAutoLoad : false,
            entityClassName : 'com.rover.crm.entity.Contracts',
            queryUrl : 'contractsController/query.action',
            deleteUrl : 'contractsController/delete.action',
            recordPk : [ "id" ],
            tbar:toolbar,
            expander:{text:'操作',
                menuDisabled: true,
                sortable: false,
                align:'center',
                xtype: 'actioncolumn',
                width: 60,
                items: [{ icon   :'../images/smallIcon/settings.png', id: 'upArraw',    tooltip: '操作',
                    handler: function(grid, rowIndex, colIndex) {   //rowIndex，colIndex均从0开始
                        var rec = grid.getStore().getAt(rowIndex);
                        grid.currentRowIndex = rowIndex;
                        var url="crm/contractsView.jsp?contractsId="+rec.raw.id;
                        mainFrame("ht"+rec.raw.id,"合同："+rec.raw.name+"：查看",url,null,0);

                    } }]}
        });
        //如果from存在，说明是在businessOpportunitiesView.jsp中打开的
        if(form)
        	{
        	grid.getStore().on('beforeload', function(store, options) {
    			store.proxy.extraParams.businessOpportunitiesId = form.getForm().findField("id").getValue();
    				}, this, true);
        	}
        
        Ext.apply(this, {items:[grid]});
        grid.load();
        this.callParent(arguments);
    } , destroy: function () {
        Ext.getCmp('contractsList').destroy();
       this.destroy();
    }
});